//
//  WDGrabOrdersPersonInfoViewModel.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/13.
//

#import "WDGrabOrdersPersonInfoViewModel.h"

@implementation WDGrabOrdersPersonInfoViewModel

- (void)initialize {
    [super initialize];
    
    self.title = @"抢单个人信息填写";
    
    _arrivalTimeOptions = @[@"30分钟内", @"1小时内", @"2小时内", @"4小时内", @"尽快达到"];
    
    _reqCancelModel = [WDRescueCancelGrabApplyReqModel new];
    _reqApplyModel = [WDRescueGrabApplyReqModel new];
    
    _reqModel = [WDTaskSetGrabLockReqModel new];
    
//    [[WDLocationManager shareInstance] updateLocation];
}

- (void)setupData {
//    _serviceModel.rescueAddress = @"济南市天桥区泺口街道泺口齐鲁内衣城";
    
    
    _reqCancelModel.engineerId = @([EDSAppDelegateTool sharedInstance].userInfoModel.id);
    _reqCancelModel.stationId = @([EDSAppDelegateTool sharedInstance].userInfoModel.stationId);
    _reqCancelModel.taskId = @(self.serviceModel.id);
    
    
    _reqModel.engineerId = @([EDSAppDelegateTool sharedInstance].userInfoModel.id);
    _reqModel.lockType = @(1);
    _reqModel.taskId = @(self.serviceModel.id);
    
//    @weakify(self);
//    [[WDLocationManager shareInstance] geocodeSearchAds:_serviceModel.rescueAddress completion:^(CGFloat latitude, CGFloat longitude) {
//        @strongify(self);
//        self.serviceModel.rescueLatitude = latitude;
//        self.serviceModel.rescueLongitude = longitude;
//    }];
}

- (void)setupReqApplyModel:(void (^)(void))successBlock {
    _reqApplyModel.engineerId = @([EDSAppDelegateTool sharedInstance].userInfoModel.id);
    _reqApplyModel.stationId = @([EDSAppDelegateTool sharedInstance].userInfoModel.stationId);
    _reqApplyModel.taskId = @(self.serviceModel.id);
    _reqApplyModel.rescueEngineerName = [EDSAppDelegateTool sharedInstance].userInfoModel.realName;
    _reqApplyModel.rescueEngineerPhone = [EDSAppDelegateTool sharedInstance].userInfoModel.phone;
    _reqApplyModel.rescueAddress = self.serviceModel.rescueAddress;
    _reqApplyModel.rescueLatitude = self.serviceModel.rescueLatitude;
    _reqApplyModel.rescueLongitude = self.serviceModel.rescueLongitude;
    _reqApplyModel.estimateDistance = [self.serviceModel.rescueDistance integerValue];
    @weakify(self);
    [[WDLocationManager shareInstance] geocodeSearchAds:_serviceModel.rescueAddress completion:^(CGFloat latitude, CGFloat longitude) {
        @strongify(self);
        self.reqApplyModel.rescueLatitude = latitude;
        self.reqApplyModel.rescueLongitude = longitude;
        [self calcTwoPointDistance:successBlock]; // 计算两点距离
    }];
}

/// 计算点之间的距离
- (void)calcTwoPointDistance:(void (^)(void))successBlock {
    double lat = [WDLocationManager shareInstance].lastLocation.coordinate.latitude;
    double lon = [WDLocationManager shareInstance].lastLocation.coordinate.longitude;
    AMapGeoPoint *currentPoint = [AMapGeoPoint locationWithLatitude:lat longitude:lon];
    AMapGeoPoint *endPoint = [AMapGeoPoint locationWithLatitude:self.reqApplyModel.rescueLatitude longitude:self.reqApplyModel.rescueLongitude];
    @weakify(self);
    [[WDLocationManager shareInstance] getTwoPointDistance:currentPoint end:endPoint completion:^(NSInteger distance) {
        @strongify(self);
        debugLog(@"距离：%zd米", distance);
        self.reqApplyModel.estimateDistance = distance;
        if (successBlock) {
            successBlock();
        }
    }];
}


/// 取消外出救援申请
/// - Parameter completion: completion description
- (void)requestWithCancelApply:(RequestCompletionBlock)completion {
    @weakify(self);
    WDRescueCancelGrabApplyRequest *request = [[WDRescueCancelGrabApplyRequest alloc] initWithModel:self.reqCancelModel];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}


/// 外出救援申请
/// - Parameter completion: completion description
- (void)requestWithApply:(RequestCompletionBlock)completion {
//    debugLog(@"model=%@", [self.reqApplyModel yy_modelToJSONString]);
    @weakify(self);
    WDRescueGrabApplyRequest *request = [[WDRescueGrabApplyRequest alloc] initWithModel:self.reqApplyModel];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

/// 设置/取消抢单锁
/// - Parameter completion: completion description
- (void)requestWithGrabLock:(RequestCompletionBlock)completion {
    @weakify(self);
    WDTaskSetGrabLockRequest *request = [[WDTaskSetGrabLockRequest alloc] initWithModel:self.reqModel];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}


@end
